package com.bbm.communitysystem.controller.waste;


import cn.hutool.core.bean.BeanUtil;
import com.bbm.communitysystem.core.domain.R;
import com.bbm.communitysystem.core.oss.OSS;
import com.bbm.communitysystem.domain.OssFile;
import com.bbm.communitysystem.domain.WasteClass;
import com.bbm.communitysystem.domain.dto.WasteClassDto;
import com.bbm.communitysystem.domain.vo.WasteClassVo;
import com.bbm.communitysystem.service.IWasteClassService;
import com.bbm.communitysystem.service.impl.OssFileServiceImpl;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author bbm
 * @since 2025-02-28
 */
@Tag(name = "废品类控制器")
@RestController
@RequestMapping("/waste-class")
@RequiredArgsConstructor
public class WasteClassController {

    private final IWasteClassService wasteClassService;

    private final OssFileServiceImpl ossFileService;

    private final OSS oss;

    @Operation(summary = "新增废品类")
    @PostMapping
    @Parameters({
            @Parameter(name = "wasteName", description = "废品类名称"),
            @Parameter(name = "wasteWeight", description = "权重值越高 排名越前"),
            @Parameter(name = "updateBy", description = "更新者")
    })
    public R<String> addWasteClass(@RequestParam("img") MultipartFile img, @RequestParam("wasteName") String wasteName,
                                   @RequestParam("wasteWeight") Integer wasteWeight, @RequestParam("updateBy") Integer updateBy) {

        if (img.isEmpty()) {
            return R.error("图片不能为空");
        }

        WasteClass wasteClass = new WasteClass();
        wasteClass.setWasteClassName(wasteName);
        wasteClass.setWasteWeight(wasteWeight);
        List<OssFile> ossFiles = oss.saveFile("waste-class", updateBy + "", img);
        wasteClass.setOssId(ossFiles.get(0).getId());
        wasteClass.setUpdateBy(updateBy + "");
        wasteClass.setStatus(1);
        wasteClass.setUpdateTime(System.currentTimeMillis() + "");

        boolean save = wasteClassService.save(wasteClass);

        return save ? R.ok("新增成功") : R.error("新增失败");
    }


    @Operation(summary = "查询所有分类")
    @GetMapping
    public R<List<WasteClassVo>> getAllClasses() {

        List<WasteClassVo> list = wasteClassService.lambdaQuery()
                .eq(WasteClass::getStatus, 1)
                .orderByDesc(WasteClass::getWasteWeight)
                .list()
                .stream()
                .map(item -> {

                    WasteClassVo wasteClassVo = BeanUtil.copyProperties(item, WasteClassVo.class);
                    OssFile byId = ossFileService.getById(item.getOssId());
                    wasteClassVo.setClientUrl(byId.getClientUrl());
                    return wasteClassVo;

                })
                .toList();


        return R.ok("获取成功", list);
    }

    @Operation(summary = "根据ID查询分类")
    @GetMapping("/{id}")
    @Parameter(name = "id", description = "分类ID")
    public R<WasteClass> getClassById(@PathVariable Long id) {
        return R.ok(wasteClassService.getById(id));
    }


    @Operation(description = "更新分类图片")
    @PutMapping("/img")
    @Parameters({
            @Parameter(name = "id", description = "分类ID")
    })
    public R<String> putWasteClassImg(@RequestParam("img") MultipartFile img, @RequestParam("id") Long id) {

        if (img.isEmpty()) {
            return R.error("图片不能为空");
        }

        WasteClass byId = wasteClassService.getById(id);

        if (byId.getOssId() == null) {
            return R.error("分类不存在");
        }

        oss.deleteFile(byId.getOssId());

        List<OssFile> ossFiles = oss.saveFile("waste-class", id + "", img);

        byId.setOssId(ossFiles.get(0).getId());

        wasteClassService.updateById(byId);

        return R.ok("修改成功");
    }

    @Operation(summary = "更新分类信息")
    @PutMapping
    public R<String> updateClass(@RequestBody WasteClassDto dto) {
        boolean success = wasteClassService.lambdaUpdate()
                .set(dto.getWasteClassName() != null && !dto.getWasteClassName().isEmpty(), WasteClass::getWasteClassName, dto.getWasteClassName())
                .set(dto.getWasteWeight() != null, WasteClass::getWasteWeight, dto.getWasteWeight())

                .set(dto.getStatus() != null, WasteClass::getStatus, dto.getStatus())
                .set(WasteClass::getUpdateTime, System.currentTimeMillis() + "")
                .eq(WasteClass::getId, dto.getId())
                .update();
        return success ? R.ok("更新成功") : R.error("更新失败");
    }

    @Operation(summary = "删除分类")
    @DeleteMapping("/{id}")
    @Parameter(name = "id", description = "分类ID")
    public R<String> deleteClass(@PathVariable Long id) {
        return wasteClassService.removeById(id) ?
                R.ok("删除成功") : R.error("删除失败");
    }

}
